home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / kdebug.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-10-10  |  17.5 KB  |  666 lines

  1. /* This file is part of the KDE libraries
  2.     Copyright (C) 1997 Matthias Kalle Dalheimer (kalle@kde.org)
  3.                   2000-2002 Stephan Kulow (coolo@kde.org)
  4.                   2002 Holger Freyther (freyther@kde.org)
  5.  
  6.     This library is free software; you can redistribute it and/or
  7.     modify it under the terms of the GNU Library General Public
  8.     License as published by the Free Software Foundation; either
  9.     version 2 of the License, or (at your option) any later version.
  10.  
  11.     This library is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.     Library General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU Library General Public License
  17.     along with this library; see the file COPYING.LIB.  If not, write to
  18.     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  19.     Boston, MA 02110-1301, USA.
  20. */
  21.  
  22. #ifndef _KDEBUG_H_
  23. #define _KDEBUG_H_
  24.  
  25. #include <qstring.h>
  26. #include "kdelibs_export.h"
  27.  
  28. class QWidget;
  29. class QDateTime;
  30. class QDate;
  31. class QTime;
  32. class QPoint;
  33. class QSize;
  34. class QRect;
  35. class QRegion;
  36. class KURL;
  37. class QStringList;
  38. class QColor;
  39. class QPen;
  40. class QBrush;
  41. class QVariant;
  42. template <class T>
  43. class QValueList;
  44.  
  45. class kdbgstream;
  46. class kndbgstream;
  47.  
  48. /**
  49.  * \addtogroup kdebug Debug message generators
  50.  *  @{
  51.  * KDE debug message streams let you and the user control just how many debug
  52.  * messages you see.
  53.  */
  54.  
  55. typedef kdbgstream & (*KDBGFUNC)(kdbgstream &); // manipulator function
  56. typedef kndbgstream & (*KNDBGFUNC)(kndbgstream &); // manipulator function
  57.  
  58. #ifdef __GNUC__
  59. #define k_funcinfo "[" << __PRETTY_FUNCTION__ << "] "
  60. #else
  61. #define k_funcinfo "[" << __FILE__ << ":" << __LINE__ << "] "
  62. #endif
  63.  
  64. #define k_lineinfo "[" << __FILE__ << ":" << __LINE__ << "] "
  65.  
  66. class kdbgstreamprivate;
  67. /**
  68.  * kdbgstream is a text stream that allows you to print debug messages.
  69.  * Using the overloaded "<<" operator you can send messages. Usually
  70.  * you do not create the kdbgstream yourself, but use kdDebug()
  71.  * kdWarning(), kdError() or kdFatal to obtain one.
  72.  *
  73.  * Example:
  74.  * \code
  75.  *    int i = 5;
  76.  *    kdDebug() << "The value of i is " << i << endl;
  77.  * \endcode
  78.  * @see kndbgstream
  79.  */
  80. class KDECORE_EXPORT kdbgstream {
  81.  public:
  82.   /**
  83.    * @internal
  84.    */
  85.     kdbgstream(unsigned int _area, unsigned int _level, bool _print = true) :
  86.       area(_area), level(_level),  print(_print) { }
  87.     kdbgstream(const char * initialString, unsigned int _area, unsigned int _level, bool _print = true) :
  88.       output(QString::fromLatin1(initialString)), area(_area), level(_level),  print(_print) { }
  89.     /// Copy constructor
  90.     kdbgstream(kdbgstream &str);
  91.     kdbgstream(const kdbgstream &str) :
  92.       output(str.output), area(str.area), level(str.level), print(str.print) {}
  93.     ~kdbgstream();
  94.     /**
  95.      * Prints the given value.
  96.      * @param i the boolean to print (as "true" or "false")
  97.      * @return this stream
  98.      */
  99.     kdbgstream &operator<<(bool i)  {
  100.     if (!print) return *this;
  101.     output += QString::fromLatin1(i ? "true" : "false");
  102.     return *this;
  103.     }
  104.     /**
  105.      * Prints the given value.
  106.      * @param i the short to print
  107.      * @return this stream
  108.      */
  109.     kdbgstream &operator<<(short i)  {
  110.     if (!print) return *this;
  111.     QString tmp; tmp.setNum(i); output += tmp;
  112.     return *this;
  113.     }
  114.     /**
  115.      * Prints the given value.
  116.      * @param i the unsigned short to print
  117.      * @return this stream
  118.      */
  119.     kdbgstream &operator<<(unsigned short i) {
  120.         if (!print) return *this;
  121.         QString tmp; tmp.setNum(i); output += tmp;
  122.         return *this;
  123.     }
  124.     /**
  125.      * Prints the given value.
  126.      * @param ch the char to print
  127.      * @return this stream
  128.      */
  129.     kdbgstream &operator<<(char ch);
  130.     /**
  131.      * Prints the given value.
  132.      * @param ch the unsigned char to print
  133.      * @return this stream
  134.      */
  135.     kdbgstream &operator<<(unsigned char ch) {
  136.         return operator<<( static_cast<char>( ch ) );
  137.     }
  138.     /**
  139.      * Prints the given value.
  140.      * @param i the int to print
  141.      * @return this stream
  142.      */
  143.     kdbgstream &operator<<(int i)  {
  144.     if (!print) return *this;
  145.     QString tmp; tmp.setNum(i); output += tmp;
  146.     return *this;
  147.     }
  148.     /**
  149.      * Prints the given value.
  150.      * @param i the unsigned int to print
  151.      * @return this stream
  152.      */
  153.     kdbgstream &operator<<(unsigned int i) {
  154.         if (!print) return *this;
  155.         QString tmp; tmp.setNum(i); output += tmp;
  156.         return *this;
  157.     }
  158.     /**
  159.      * Prints the given value.
  160.      * @param i the long to print
  161.      * @return this stream
  162.      */
  163.     kdbgstream &operator<<(long i) {
  164.         if (!print) return *this;
  165.         QString tmp; tmp.setNum(i); output += tmp;
  166.         return *this;
  167.     }
  168.     /**
  169.      * Prints the given value.
  170.      * @param i the unsigned long to print
  171.      * @return this stream
  172.      */
  173.     kdbgstream &operator<<(unsigned long i) {
  174.         if (!print) return *this;
  175.         QString tmp; tmp.setNum(i); output += tmp;
  176.         return *this;
  177.     }
  178.     /**
  179.      * Prints the given value.
  180.      * @param i the long long to print
  181.      * @return this stream
  182.      */
  183.     kdbgstream &operator<<(Q_LLONG i) {
  184.         if (!print) return *this;
  185.         QString tmp; tmp.setNum(i); output += tmp;
  186.         return *this;
  187.     }
  188.     /**
  189.      * Prints the given value.
  190.      * @param i the unsigned long long to print
  191.      * @return this stream
  192.      */
  193.     kdbgstream &operator<<(Q_ULLONG i) {
  194.         if (!print) return *this;
  195.         QString tmp; tmp.setNum(i); output += tmp;
  196.         return *this;
  197.     }
  198.  
  199.     /**
  200.      * Flushes the output.
  201.      */
  202.     void flush(); //AB: maybe this should be virtual! would save some trouble for some 3rd party projects
  203.  
  204.     /**
  205.      * Prints the given value.
  206.      * @param ch the char to print
  207.      * @return this stream
  208.      * @since 3.3
  209.      */
  210.     kdbgstream &operator<<(QChar ch);
  211.     /**
  212.      * Prints the given value.
  213.      * @param string the string to print
  214.      * @return this stream
  215.      */
  216.     kdbgstream &operator<<(const QString& string) {
  217.     if (!print) return *this;
  218.     output += string;
  219.     if (output.at(output.length() -1 ) == '\n')
  220.         flush();
  221.     return *this;
  222.     }
  223.     /**
  224.      * Prints the given value.
  225.      * @param string the string to print
  226.      * @return this stream
  227.      */
  228.     kdbgstream &operator<<(const char *string) {
  229.     if (!print) return *this;
  230.     output += QString::fromUtf8(string);
  231.     if (output.at(output.length() - 1) == '\n')
  232.         flush();
  233.     return *this;
  234.     }
  235.     /**
  236.      * Prints the given value.
  237.      * @param string the string to print
  238.      * @return this stream
  239.      */
  240.     kdbgstream &operator<<(const QCString& string) {
  241.         *this << string.data();
  242.         return *this;
  243.     }
  244.     /**
  245.      * Prints the given value.
  246.      * @param p a pointer to print (in number form)
  247.      * @return this stream
  248.      */
  249.     kdbgstream& operator<<(const void * p) {
  250.         form("%p", p);
  251.         return *this;
  252.     }
  253.     /**
  254.      * Invokes the given function.
  255.      * @param f the function to invoke
  256.      * @return the return value of @p f
  257.      */
  258.     kdbgstream& operator<<(KDBGFUNC f) {
  259.     if (!print) return *this;
  260.     return (*f)(*this);
  261.     }
  262.     /**
  263.      * Prints the given value.
  264.      * @param d the double to print
  265.      * @return this stream
  266.      */
  267.     kdbgstream& operator<<(double d) {
  268.       QString tmp; tmp.setNum(d); output += tmp;
  269.       return *this;
  270.     }
  271.     /**
  272.      * Prints the string @p format which can contain
  273.      * printf-style formatted values.
  274.      * @param format the printf-style format
  275.      * @return this stream
  276.      */
  277.     kdbgstream &form(const char *format, ...)
  278. #ifdef __GNUC__
  279.       __attribute__ ( ( format ( printf, 2, 3 ) ) )
  280. #endif
  281.      ;
  282.  
  283.     /** Operator to print out basic information about a QWidget.
  284.      *  Output of class names only works if the class is moc'ified.
  285.      * @param widget the widget to print
  286.      * @return this stream
  287.      */
  288.     kdbgstream& operator << (const QWidget* widget);
  289.     kdbgstream& operator << (QWidget* widget); // KDE4 merge
  290.  
  291.     /**
  292.      * Prints the given value.
  293.      * @param dateTime the datetime to print
  294.      * @return this stream
  295.      */
  296.     kdbgstream& operator << ( const QDateTime& dateTime );
  297.  
  298.     /**
  299.      * Prints the given value.
  300.      * @param date the date to print
  301.      * @return this stream
  302.      */
  303.     kdbgstream& operator << ( const QDate& date );
  304.  
  305.     /**
  306.      * Prints the given value.
  307.      * @param time the time to print
  308.      * @return this stream
  309.      */
  310.     kdbgstream& operator << ( const QTime& time );
  311.  
  312.     /**
  313.      * Prints the given value.
  314.      * @param point the point to print
  315.      * @return this stream
  316.      */
  317.     kdbgstream& operator << ( const QPoint& point );
  318.  
  319.     /**
  320.      * Prints the given value.
  321.      * @param size the QSize to print
  322.      * @return this stream
  323.      */
  324.     kdbgstream& operator << ( const QSize& size );
  325.  
  326.     /**
  327.      * Prints the given value.
  328.      * @param rect the QRect to print
  329.      * @return this stream
  330.      */
  331.     kdbgstream& operator << ( const QRect& rect);
  332.  
  333.     /**
  334.      * Prints the given value.
  335.      * @param region the QRegion to print
  336.      * @return this stream
  337.      */
  338.     kdbgstream& operator << ( const QRegion& region);
  339.  
  340.     /**
  341.      * Prints the given value.
  342.      * @param url the url to print
  343.      * @return this stream
  344.      */
  345.     kdbgstream& operator << ( const KURL& url );
  346.  
  347.     /**
  348.      * Prints the given value.
  349.      * @param list the stringlist to print
  350.      * @return this stream
  351.      */
  352.     // ### KDE4: Remove in favor of template operator for QValueList<T> below
  353.     kdbgstream& operator << ( const QStringList& list);
  354.  
  355.     /**
  356.      * Prints the given value.
  357.      * @param color the color to print
  358.      * @return this stream
  359.      */
  360.     kdbgstream& operator << ( const QColor& color);
  361.  
  362.     /**
  363.      * Prints the given value.
  364.      * @param pen the pen to print
  365.      * @return this stream
  366.      * @since 3.2
  367.      */
  368.     kdbgstream& operator << ( const QPen& pen );
  369.  
  370.     /**
  371.      * Prints the given value.
  372.      * @param brush the brush to print
  373.      * @return this stream
  374.      */
  375.     kdbgstream& operator << ( const QBrush& brush );
  376.  
  377.     /**
  378.      * Prints the given value.
  379.      * @param variant the variant to print
  380.      * @return this stream
  381.      * @since 3.3
  382.      */
  383.     kdbgstream& operator << ( const QVariant& variant );
  384.  
  385.     /**
  386.      * Prints the given value.
  387.      * @param data the byte array to print
  388.      * @return this stream
  389.      * @since 3.3
  390.      */
  391.     kdbgstream& operator << ( const QByteArray& data );
  392.  
  393.     /**
  394.      * Prints the given value
  395.      * @param list the list to print
  396.      * @return this stream
  397.      * @since 3.3
  398.      */
  399.     template <class T>
  400.     kdbgstream& operator << ( const QValueList<T> &list );
  401.  
  402.  private:
  403.     QString output;
  404.     unsigned int area, level;
  405.     bool print;
  406.     kdbgstreamprivate* d;
  407. };
  408.  
  409. template <class T>
  410. kdbgstream &kdbgstream::operator<<( const QValueList<T> &list )
  411. {
  412.     *this << "(";
  413.     typename QValueList<T>::ConstIterator it = list.begin();
  414.     if ( !list.isEmpty() ) {
  415.       *this << *it++;
  416.     }
  417.     for ( ; it != list.end(); ++it ) {
  418.       *this << "," << *it;
  419.     }
  420.     *this << ")";
  421.     return *this;
  422. }
  423.  
  424. /**
  425.  * \relates KGlobal
  426.  * Prints an "\n".
  427.  * @param s the debug stream to write to
  428.  * @return the debug stream (@p s)
  429.  */
  430. inline kdbgstream &endl( kdbgstream &s) { s << "\n"; return s; }
  431.  
  432. /**
  433.  * \relates KGlobal
  434.  * Flushes the stream.
  435.  * @param s the debug stream to write to
  436.  * @return the debug stream (@p s)
  437.  */
  438. inline kdbgstream &flush( kdbgstream &s) { s.flush(); return s; }
  439.  
  440. KDECORE_EXPORT kdbgstream &perror( kdbgstream &s);
  441.  
  442. /**
  443.  * \relates KGlobal
  444.  * kndbgstream is a dummy variant of kdbgstream. All functions do
  445.  * nothing.
  446.  * @see kndDebug()
  447.  */
  448. class KDECORE_EXPORT kndbgstream {
  449.  public:
  450.     /// Default constructor.
  451.     kndbgstream() {}
  452.     ~kndbgstream() {}
  453.     /**
  454.      * Does nothing.
  455.      * @return this stream
  456.      */
  457.     kndbgstream &operator<<(short int )  { return *this; }
  458.     /**
  459.      * Does nothing.
  460.      * @return this stream
  461.      */
  462.     kndbgstream &operator<<(unsigned short int )  { return *this; }
  463.     /**
  464.      * Does nothing.
  465.      * @return this stream
  466.      */
  467.     kndbgstream &operator<<(char )  { return *this; }
  468.     /**
  469.      * Does nothing.
  470.      * @return this stream
  471.      */
  472.     kndbgstream &operator<<(unsigned char )  { return *this; }
  473.     /**
  474.      * Does nothing.
  475.      * @return this stream
  476.      */
  477.     kndbgstream &operator<<(int )  { return *this; }
  478.     /**
  479.      * Does nothing.
  480.      * @return this stream
  481.      */
  482.     kndbgstream &operator<<(unsigned int )  { return *this; }
  483.     /**
  484.      * Does nothing.
  485.      */
  486.     void flush() {}
  487.     /**
  488.      * Does nothing.
  489.      * @return this stream
  490.      */
  491.     kndbgstream &operator<<(QChar)  { return *this; }
  492.     /**
  493.      * Does nothing.
  494.      * @return this stream
  495.      */
  496.     kndbgstream &operator<<(const QString& ) { return *this; }
  497.     /**
  498.      * Does nothing.
  499.      * @return this stream
  500.      */
  501.     kndbgstream &operator<<(const QCString& ) { return *this; }
  502.     /**
  503.      * Does nothing.
  504.      * @return this stream
  505.      */
  506.     kndbgstream &operator<<(const char *) { return *this; }
  507.     /**
  508.      * Does nothing.
  509.      * @return this stream
  510.      */
  511.     kndbgstream& operator<<(const void *) { return *this; }
  512.     /**
  513.      * Does nothing.
  514.      * @return this stream
  515.      */
  516.     kndbgstream& operator<<(void *) { return *this; }
  517.     /**
  518.      * Does nothing.
  519.      * @return this stream
  520.      */
  521.     kndbgstream& operator<<(double) { return *this; }
  522.     /**
  523.      * Does nothing.
  524.      * @return this stream
  525.      */
  526.     kndbgstream& operator<<(long) { return *this; }
  527.     /**
  528.      * Does nothing.
  529.      * @return this stream
  530.      */
  531.     kndbgstream& operator<<(unsigned long) { return *this; }
  532.     /**
  533.      * Does nothing.
  534.      * @return this stream
  535.      */
  536.     kndbgstream& operator<<(Q_LLONG) { return *this; }
  537.     /**
  538.      * Does nothing.
  539.      * @return this stream
  540.      */
  541.     kndbgstream& operator<<(Q_ULLONG) { return *this; }
  542.     /**
  543.      * Does nothing.
  544.      * @return this stream
  545.      */
  546.     kndbgstream& operator<<(KNDBGFUNC) { return *this; }
  547.     /**
  548.      * Does nothing.
  549.      * @return this stream
  550.      */
  551.     kndbgstream& operator << (const QWidget*) { return *this; }
  552.     kndbgstream& operator << (QWidget*) { return *this; } // KDE4 merge
  553.     /**
  554.      * Does nothing.
  555.      * @return this stream
  556.      */
  557.     kndbgstream &form(const char *, ...) { return *this; }
  558.  
  559.     kndbgstream& operator<<( const QDateTime& ) { return *this; }
  560.     kndbgstream& operator<<( const QDate&     ) { return *this; }
  561.     kndbgstream& operator<<( const QTime&     ) { return *this; }
  562.     kndbgstream& operator<<( const QPoint & )  { return *this; }
  563.     kndbgstream& operator<<( const QSize & )  { return *this; }
  564.     kndbgstream& operator<<( const QRect & )  { return *this; }
  565.     kndbgstream& operator<<( const QRegion & ) { return *this; }
  566.     kndbgstream& operator<<( const KURL & )  { return *this; }
  567.     kndbgstream& operator<<( const QStringList & ) { return *this; }
  568.     kndbgstream& operator<<( const QColor & ) { return *this; }
  569.     kndbgstream& operator<<( const QPen & ) { return *this; }
  570.     kndbgstream& operator<<( const QBrush & ) { return *this; }
  571.     kndbgstream& operator<<( const QVariant & ) { return *this; }
  572.     kndbgstream& operator<<( const QByteArray & ) { return *this; }
  573.  
  574.     template <class T>
  575.     kndbgstream& operator<<( const QValueList<T> & ) { return *this; }
  576. };
  577.  
  578. /**
  579.  * Does nothing.
  580.  * @param s a stream
  581.  * @return the given @p s
  582.  */
  583. inline kndbgstream &endl( kndbgstream & s) { return s; }
  584. /**
  585.  * Does nothing.
  586.  * @param s a stream
  587.  * @return the given @p s
  588.  */
  589. inline kndbgstream &flush( kndbgstream & s) { return s; }
  590. inline kndbgstream &perror( kndbgstream & s) { return s; }
  591.  
  592. /**
  593.  * \relates KGlobal
  594.  * Returns a debug stream. You can use it to print debug
  595.  * information.
  596.  * @param area an id to identify the output, 0 for default
  597.  * @see kndDebug()
  598.  */
  599. KDECORE_EXPORT kdbgstream kdDebug(int area = 0);
  600. KDECORE_EXPORT kdbgstream kdDebug(bool cond, int area = 0);
  601. /**
  602.  * \relates KGlobal
  603.  * Returns a backtrace.
  604.  * @return a backtrace
  605.  */
  606. KDECORE_EXPORT QString kdBacktrace();
  607. /**
  608.  * \relates KGlobal
  609.  * Returns a backtrace.
  610.  * @param levels the number of levels of the backtrace
  611.  * @return a backtrace
  612.  * @since 3.1
  613.  */
  614. KDECORE_EXPORT QString kdBacktrace(int levels);
  615. /**
  616.  * Returns a dummy debug stream. The stream does not print anything.
  617.  * @param area an id to identify the output, 0 for default
  618.  * @see kdDebug()
  619.  */
  620. inline kndbgstream kndDebug(int area = 0) { Q_UNUSED(area); return kndbgstream(); }
  621. inline kndbgstream kndDebug(bool , int  = 0) { return kndbgstream(); }
  622. inline QString kndBacktrace() { return QString::null; }
  623. inline QString kndBacktrace(int) { return QString::null; }
  624.  
  625. /**
  626.  * \relates KGlobal
  627.  * Returns a warning stream. You can use it to print warning
  628.  * information.
  629.  * @param area an id to identify the output, 0 for default
  630.  */
  631. KDECORE_EXPORT kdbgstream kdWarning(int area = 0);
  632. KDECORE_EXPORT kdbgstream kdWarning(bool cond, int area = 0);
  633. /**
  634.  * \relates KGlobal
  635.  * Returns an error stream. You can use it to print error
  636.  * information.
  637.  * @param area an id to identify the output, 0 for default
  638.  */
  639. KDECORE_EXPORT kdbgstream kdError(int area = 0);
  640. KDECORE_EXPORT kdbgstream kdError(bool cond, int area = 0);
  641. /**
  642.  * \relates KGlobal
  643.  * Returns a fatal error stream. You can use it to print fatal error
  644.  * information.
  645.  * @param area an id to identify the output, 0 for default
  646.  */
  647. KDECORE_EXPORT kdbgstream kdFatal(int area = 0);
  648. KDECORE_EXPORT kdbgstream kdFatal(bool cond, int area = 0);
  649.  
  650. /**
  651.  * \relates KGlobal
  652.  * Deletes the kdebugrc cache and therefore forces KDebug to reread the
  653.  * config file
  654.  */
  655. KDECORE_EXPORT void kdClearDebugConfig();
  656.  
  657. /** @} */
  658.  
  659. #ifdef NDEBUG
  660. #define kdDebug kndDebug
  661. #define kdBacktrace kndBacktrace
  662. #endif
  663.  
  664. #endif
  665.  
  666.